const express =require("express");
const router=express.Router();
const {Shop} =require("../model/model");

//查询所有
router.get("/findShopAll",(req,res)=>{
    Shop.find({},function (err,shop) {
    if(err){
        console.log("没有对应数据！",+err);
    }else{
        res.send({
            code:200,
            msg:"查询成功！",
            result:shop
        })
    }
 })
});

//按id修改
router.post("/updShop/:id",(req,res)=>{
    let obj={
        num:req.body.num,
        type:req.body.type,
        shop_name:req.body.shop_name,
        price:req.body.price,
        count:req.body.count,
        stock:req.body.stock
    };
Shop.updateMany({_id:req.params.id},{$set:obj},function (err,shop) {
    if(err){
        console.log("修改失败")
    }else{
        res.send({
            code:200,
            msg:"修改成功！",
            result:shop
        })
    }
})
});
//增加数据
router.post("/addShop",(req,res)=>{
    new Shop(req.body).save(function(err,shop){
        if(err){
            res.send({
                code:-1,
                msg:'数据库连接错误'
            })
        }else{
            res.send({
                code:200,
                msg:'添加学生信息成功',
                data:{
                    shop:shop
                }
            })
        }
    })
});
//删除数据
router.post("/delShop/:id",(req,res)=>{
    new Shop({_id:req.params.id}).remove(function(err,shop){
        if(err){
            res.send({
                code:-1,
                msg:'数据库连接错误'
            })
        }else{
            res.send({
                code:200,
                msg:'ok',
                data:{
                    shop:shop
                }
            })
        }
    })
});
module.exports=router;